<template>
  <el-container>
    <el-header><Header></Header></el-header>
    <el-container>
      <el-aside width="200px">
        <Aside></Aside>
      </el-aside>
      <el-main>
        <h3 style="text-align: center">修改密码</h3>
        <el-form
          ref="form"
          :model="form"
          label-width="100px"
          style="width: 600px; margin: 0 auto"
        >
          <el-form-item label="旧密码">
            <el-input type="password" v-model="form.old_password"></el-input>
          </el-form-item>

          <el-form-item label="新密码">
            <el-input type="password" v-model="form.new_password"></el-input>
          </el-form-item>

          <el-form-item label="再次新密码">
            <el-input type="password" v-model="form.new_password2"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="onSubmit">提交</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
    <el-footer><Footer></Footer></el-footer>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      form: {
        old_password: "",
        new_password: "",
        new_password2: "",
      },
    };
  },

  methods: {
    async onSubmit() {
      if (!this.form.old_password) {
        this.$message({
          type: "error",
          message: "旧密码不能为空",
        });
        return;
      }
      if (!this.form.new_password) {
        this.$message({
          type: "error",
          message: "新密码不能为空",
        });
        return;
      }
      if (!this.form.new_password2) {
        this.$message({
          type: "error",
          message: "再次新密码不能为空",
        });
        return;
      }
      if (this.form.new_password != this.form.new_password2) {
        this.$message({
          type: "error",
          message: "两次新密码不一致",
        });
        return;
      }
      try {
        let data = await this.$axios.post(
          "/api/safe/change_password",
          this.form
        );
        if (parseInt(data.code) > 0) {
          this.$message({
            message: data.msg,
            type: "success",
            duration: 1000,
          });
          this.form.old_password = "";
          this.form.new_password = "";
          this.form.new_password2 = "";
        } else {
          this.$message.error(data.msg);
        }
      } catch (error) {
        this.$message.error(error.toString());
      }
    },
  },
};
</script>

<style scoped>
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
}

.el-main {
  
  color: #333;
}
</style>
